﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NanoERP.Business.Permission;
using NanoERP.Model.Poco;
using LL.Utility.WindowsForms;
using NanoERP.Infrastructure;
using NanoERP.Model.SystemMgmt;

namespace NanoERP.Modules.SystemMgmt
{
    [Module("FrmMgrModule")]
    public partial class FrmMgrModule : ModuleForm
    {
        private SelectedType _selectedType;
        private SelectedType SelectedType
        {
            get { return _selectedType; }
            set
            {
                _selectedType = value;
                ChangeUiState();
            }
        }

        public FrmMgrModule()
        {
            InitializeComponent();
            InitControls();
            LoadData();
        }

        private void InitControls()
        {
            InitTreeView();
            ChangeUiState();
        }

        private void InitTreeView()
        {
            this.treeView1.ImageList = new ImageList();
            this.treeView1.ImageList.Images.Add(
                "folder_classic", Properties.Resources.folder_classic);
            this.treeView1.ImageList.Images.Add(
                "folder_classic_opened", Properties.Resources.folder_classic_opened);
            this.treeView1.ImageList.Images.Add(
                "group_blue", Properties.Resources.group_blue);
            this.treeView1.ImageList.Images.Add(
                "group_blue_remove", Properties.Resources.group_blue_remove);

            var tvHelper = new TreeViewHelper(this.treeView1);
            tvHelper.EnableCheckAll();
        }

        private void ChangeUiState()
        {
            foreach (ToolStripItem item in toolStrip1.Items)
            {
                item.Enabled = false;
            }
            switch (this.SelectedType)
            {
                case SelectedType.Department:
                    tsAddDep.Enabled = true;
                    tsAddUser.Enabled = true;
                    tsEdit.Enabled = true;
                    tsDel.Enabled = true;
                    break;
                case SelectedType.User:
                    tsEdit.Enabled = true;
                    tsDel.Enabled = true;
                    //tsAuth.Enabled = true;
                    tsFunction.Enabled = true;
                    break;
                default:
                    tsAddDep.Enabled = true;
                    tsAddUser.Enabled = true;
                    break;
            }
        }

        public void LoadData()
        {
            this.treeView1.Nodes.Clear();
            var list = new UserService().GetDepTreeWithUsers();
            var parent = this.treeView1.Nodes;
            BuildNode(parent, list);
            if (this.treeView1.Nodes[0] != null)
                this.treeView1.Nodes[0].ExpandAll();
        }

        #region 静态方法

        private static void BuildNode(TreeNodeCollection parent, IEnumerable<DepartmentTree> list)
        {
            foreach (var departmentModel in list)
            {
                var node = new TreeNode();
                node.Text = departmentModel.Name;
                node.Tag = departmentModel;
                node.SelectedImageKey = "folder_classic_opened";
                BuildNode(node.Nodes, departmentModel.Children);
                BuildUserNode(node.Nodes, departmentModel.Users);
                parent.Add(node);
            }
        }

        private static void BuildUserNode(TreeNodeCollection node, IEnumerable<UserModel> users)
        {
            foreach (var user in users)
            {
                var childNode = new TreeNode();
                childNode.Text = user.Name;
                childNode.Tag = user;
                childNode.SelectedImageKey = "group_blue";
                childNode.ImageKey = "group_blue";
                node.Add(childNode);
            }
        }

        #endregion

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node == treeView1.Nodes[0])
            {
                SelectedType = SelectedType.None;
                return;
            }
            if (e.Node.Tag as DepartmentTree != null)
            {  
                SelectedType = SelectedType.Department;

                var frm = new FrmEditDepartment()
                {
                    TopLevel = false,
                    Data = e.Node.Tag as SYS_Department,
                    Dock = DockStyle.Fill,
                    FormBorderStyle = FormBorderStyle.None
                };
                frm.Enabled = false;
                panel1.Controls.Clear();
                panel1.Controls.Add(frm);
                frm.Show();
            }
            else if (e.Node.Tag as UserModel != null)
            {  
                SelectedType = SelectedType.User;
                
                var frm = new FrmEditUser()
                {
                    TopLevel = false,
                    Data = e.Node.Tag as SYS_User,
                    Dock = DockStyle.Fill,
                    FormBorderStyle = FormBorderStyle.None
                };
                frm.Enabled = false;
                panel1.Controls.Clear();
                panel1.Controls.Add(frm);
                frm.Show();
            }
        }

        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.CancelEdit || e.Label == null)
                return;
            if (e.Node.Tag is DepartmentTree)
            {
                var dep = e.Node.Tag as DepartmentTree;
                dep.Name = e.Label.Trim();

                new DepartmentService().UpdateDepartment(dep);
            }
        }

        void tvHelper_DropCompleted(TreeNode destinationNode, TreeNode sourceNode)
        {
            var dep = destinationNode.Tag as DepartmentTree;
            var dep2 = sourceNode.Tag as DepartmentTree;
            var user = sourceNode.Tag as UserModel;
            Guid? desId = dep == null ? null : new Guid?(dep.Id);
            if (dep2 != null)
            {
                using (var depBO = new DepartmentService())
                    depBO.Move(dep2.Id, desId);
                return;
            }
            new UserService().MoveToDepartment(user.Id, desId);
        }

        #region 操作

        private void tsAddDep_Click(object sender, EventArgs e)
        {

        }

        private void tsAddUser_Click(object sender, EventArgs e)
        {
            //DataFormBase frm = null;

            //frm = new FrmEditUser();
            //frm.OnSavingData += (d) =>
            //{
            //    var info = (SYS_User)d;
            //    new UserService().AddUser(info);
            //    LoadData();
            //    return true;
            //};
            //var newUser = new SYS_User();
            //newUser.Id = Guid.NewGuid();
            //if (SelectedType == SelectedType.Department)
            //    newUser.DepartmentId = (treeView1.SelectedNode.Tag as SYS_Department).Id;
            //frm.DataSource = newUser;
            //frm.ShowDialog(this);
        }

        private void tsEdit_Click(object sender, EventArgs e)
        {
            EditFormBase frm = null;
            if (SelectedType == SelectedType.Department)
            {
                frm = new FrmEditDepartment()
                {
                    Data = treeView1.SelectedNode.Tag as SYS_Department
                };
            }
            else
            {
                frm = new FrmEditUser()
                {
                    Data = treeView1.SelectedNode.Tag as SYS_User
                };
            }
            frm.ShowDialog(this);
        }

        private void tsDel_Click(object sender, EventArgs e)
        {

        }

        private void tsFunction_Click(object sender, EventArgs e)
        {
            var frm = new FrmSelectRole()
            {
                Data = treeView1.SelectedNode.Tag as SYS_User
            };
            frm.ShowDialog(this);
        }


        #endregion

    }
}
